package leetcode_2301_2400;

public class LeeCode_2321 {
    public static void main(String[] args) {
        System.out.println(maximumsSplicedArray(new int[]{60, 60, 60}, new int[]{10, 90, 10}));
    }
    private static int maximumsSplicedArray(int[] nums1, int[] nums2) {
        int n = nums1.length;
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            diff[i] = nums1[i] - nums2[i];
        }
        int sum = 0, max = 0;
        for (int i = 0; i < n; i++) {
            sum += diff[i];
            if (sum < 0){
                sum = 0;
                continue;
            }
            max = Math.max(max, sum);
        }
        int s1 = 0, s2 = 0;
        for (int i = 0; i < n; i++) {
            s1 += nums1[i];
            s2 += nums2[i];
        }
        int ans = s2 + max;

        sum = 0;
        max = 0;
        for (int i = 0; i < n; i++) {
            diff[i] *= -1;
        }
        for (int i = 0; i < n; i++) {
            sum += diff[i];
            if (sum < 0){
                sum = 0;
                continue;
            }
            max = Math.max(max, sum);
        }
        ans = Math.max(ans, s1 + max);
        return ans;
    }
}
